package com.glory.eapequipmentmonitor.activity;

import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.base.Constant;
import com.glory.eapequipmentmonitor.bean.PackBean;
import com.glory.eapequipmentmonitor.fragment.AlreadyDoneFragment;
import com.glory.eapequipmentmonitor.fragment.NotDoneFragment;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabEntity;
import com.pda.platform.ui.ui_pdaplatform.tablayout.CommonTabLayout;
import com.pda.platform.ui.ui_pdaplatform.tablayout.listener.CustomTabEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PackPackageActivity extends FreeUI_BaseActivity {


    public String packType = "SmallBoxPackageType";
    @BindView(R.id.etLotId)
    public FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.tvLotTitle)
    TextView tvLotTitle;
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rbSmallBox)
    RadioButton rbSmallBox;
    @BindView(R.id.rbBox)
    RadioButton rbBox;
    @BindView(R.id.rbJuan)
    RadioButton rbJuan;

    private AlreadyDoneFragment alreadyDoneFragment;
    private NotDoneFragment notDoneFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pack_package;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        notDoneFragment = new NotDoneFragment();
        mFragments.add(notDoneFragment);
        alreadyDoneFragment = new AlreadyDoneFragment();
        mFragments.add(alreadyDoneFragment);
        mTabEntities.add(new FreeUI_TabEntity("未包装",
                null, null));
        mTabEntities.add(new FreeUI_TabEntity("已包装",
                null, null));
        tabLayout.setTabData(mTabEntities, this, R.id.content, mFragments);
    }

    @Override
    protected void initData() {

        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    boolean isHave = false;
                    for (PackBean entity : notDoneFragment.packListAdapter.getData()) {
                        if (entity.getLOTID().equalsIgnoreCase(s.toString())) {
                            isHave = true;
                            break;
                        }
                    }
                    if (isHave) {
                        showToast("批次号已存在列表中", false);
                    } else {
                        requestLotData(s.toString());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void requestLotData(String lotId) {
        PackBean packBean = new PackBean();
        packBean.setLOTID(lotId);
        packBean.setMAINQTY(3000);
        packBean.setPARTNAME(lotId + "-" + 3000);
        notDoneFragment.packListAdapter.addData(0, packBean);
        etLotId.requestFocus();
        etLotId.setText("");
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("包装");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    public void setAlreadyDoneList(List<PackBean> list) {
        alreadyDoneFragment.packListAdapter.addData(0, list);
    }

    public void setNotDoneList(List<PackBean> list) {
        for (PackBean obj : list) {
            for (PackBean packBean : notDoneFragment.packListAdapter.getData()) {
                if (packBean.getLOTID().equals(obj.getLOTID())) {
                    packBean.setMAINQTY(obj.getMAINQTY());
                }
            }
        }
        notDoneFragment.packListAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.rbSmallBox, R.id.rbBox, R.id.rbJuan})
    public void onViewClicked(View view) {
        boolean isOk = true;
        if (notDoneFragment.packListAdapter.getData().size() > 0 || alreadyDoneFragment.packListAdapter.getData().size() > 0) {
            isOk = false;
            showToast("列表中存在批次信息，不能更换", false);
        }
        switch (view.getId()) {
            case R.id.rbSmallBox:
                if (isOk) {
                    packType = "SmallBoxPackageType";
                } else {
                    setCheck();
                }
                break;
            case R.id.rbBox:
                if (isOk) {
                    packType = "BoxPackageType";
                } else {
                    setCheck();
                }
                break;
            case R.id.rbJuan:
                if (isOk) {
                    packType = "ReelPack";
                } else {
                    setCheck();
                }
                break;
        }
    }

    private void setCheck() {
        switch (packType) {
            case "SmallBoxPackageType":
                rbSmallBox.setChecked(true);
                break;
            case "BoxPackageType":
                rbBox.setChecked(true);
                break;
            case "ReelPack":
                rbJuan.setChecked(true);
                break;
        }
    }
}
